package com.aviccxzx.smarthome.utils;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.text.TextPaint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.aviccxzx.smarthome.view.TextMoveLayout;


/**
 * Created by joey on 2016/11/10.
 */
public class SeekBarUtil {
    private SeekBar mSeekbar = null;
    private TextView moveText;

    private int screenWidth;//屏幕宽度
    private ViewGroup.LayoutParams layoutParams;

    private int mLeftMargin;
    private TextPaint mPaint;
    private TextView mTvMax;
    private LinearLayout mParentView;
    private Boolean firstIn = false;
    private TextMoveLayout textMoveLayout;
    private Context mContext;




    public SeekBarUtil(SeekBar mSeekbar, LinearLayout mParentView, TextView mTvMax, int screenWidth, Context mContext, TextMoveLayout textMoveLayout) {
        this.mSeekbar = mSeekbar;
        this.mParentView = mParentView;
        this.mTvMax = mTvMax;
        this.screenWidth = screenWidth;
        this.mContext = mContext;
        this.textMoveLayout = textMoveLayout;
        firstIn = true;
        setMoveTextView();
        getChildenLayoutParams();
        setSeekBarValues();
    }


    /**
     * 设置moveTextView的基础参数
     */
    private void setMoveTextView() {

        moveText = new TextView(mContext);
        moveText.setText(0 + "");
        moveText.setTextColor(Color.rgb(0, 161, 229));
        moveText.setTextSize(16);

        layoutParams = new ViewGroup.LayoutParams(screenWidth, 50);

//        textMoveLayout = (TextMoveLayout) findViewById(R.id.textLayout);

        textMoveLayout.addView(moveText, layoutParams);
        moveText.layout(5, 20, screenWidth, 80);
    }

    /**
     * 获取子view的marginLayoutParams
     */
    private void getChildenLayoutParams() {
        View childAt = mParentView.getChildAt(0);
        ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) childAt.getLayoutParams();
        mLeftMargin = marginLayoutParams.leftMargin;
        mSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListenerImp());
    }
    /**
     * 设置seekbar的基础参数
     */
    public void setSeekBarValues() {

        //        mTvMax.setText("100");
        mSeekbar.setEnabled(true);
        mSeekbar.setMax(100);
        mSeekbar.setProgress(100);
    }

    /**
     * seekbar滑动监听
     */
    private class OnSeekBarChangeListenerImp implements
            SeekBar.OnSeekBarChangeListener {

        // 触发操作，拖动
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            setMoveTextLayout();
        }

        // 开始拖动时候触发的操作
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        // 停止拖动时候
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    }

    /**
     * seekbar滑动过程中改变moveTextView的位置
     */
    private void setMoveTextLayout() {
        if (mPaint == null) {
            mPaint = new TextPaint();
        }

        float measureText = mPaint.measureText(moveText.getText().toString().trim());
        Rect bounds = mSeekbar.getProgressDrawable().getBounds();
        int xFloat = (int) (bounds.width() * mSeekbar.getProgress() / mSeekbar.getMax() - measureText / 2 + DensityUtil.px2dip(mContext, mLeftMargin));
        if(firstIn){
            firstIn = false;
            moveText.layout(624, 20, screenWidth, 80);
            moveText.setText(100 + "");
        }else {
            moveText.layout(xFloat, 20, screenWidth, 80);
            moveText.setText(mSeekbar.getProgress() + "");
        }

    }

}
